Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 Search this book:
Previous | Table of Contents | Next
TestExpression is any numeric or string expression. Select Case goes through the list of Case statements, comparing TestExpression with each Comparison until a match is found. At that point, the statements in the associated block are executed. If no match is found, the statements associated with the optional Case Else statement are executed. If there is no Case Else and no match, none of the statements is executed. The number of Case statements allowed in a Select Case structure has no limit. In the simplest situation, each Comparison is a numeric or string expression against which TestExpression is compared for equality. You can also use the To keyword to check TestExpression against a range of values, and the Is keyword in conjunction with one of the comparison operators to make a relational comparison. Thus, if you want a match when TestExpression is between 1 and 5, you would write
    Case 1 To 5
and if you want a match when TestExpression is greater than 10, you would write:
    Case Is > 10
You can use multiple Comparison expressions that are associated with one Case statement by separating them with commas. For example, here’s a Case statement that would match TestExpression if it is equal to -1 or -2, between 8 and 12, or greater than 100:
    Case -1, -2, 8 To 12, Is > 100
#### Loop Structures You’ve seen that Basic’s decision structures determine whether a block of statements is executed. In contrast, Basic’s loop structures control how many times a block of statements is executed. You can accomplish “looping” in one of two ways: by executing a block of statements a fixed number of times or by executing the block repeatedly until a specified condition is met. For...Next In its most common use, the For...Next loop executes a block of statements a fixed number of times:
    For Counter = Start To Stop
       ...
       statement block
       ...
    Next Counter
Counter is a numeric variable that can be any type, although you would generally use type Integer unless you have a specific reason to use another type. Start and Stop are the values that specify the start and stop of the loop; they can be any numeric expression. A For...Next loop begins by setting Counter equal to Start. It then follows these steps: 1. Counter is compared with Stop. If Counter is greater than Stop, execution passes to the first statement after the For…Next loop. 2. The statements in the loop are executed. 3. Counter is incremented by 1. 4. Return to Step 1. Note that if Stop is equal to Start, the loop executes only once; if Stop is less than Start, it does not execute at all. TIP: Never Change The Counter Variable Basic will not prevent you from changing the value of the Counter variable inside the loop. This practice should be avoided, however. It can lead to pesky program bugs and code that is difficult to understand. Here’s an example of a For...Next loop that will execute the number of times specified by the variable X.
    For Count = 1 to X
       ...
    Next X
You are not limited to starting the loop at 1, of course. You can also use the Step keyword to specify that the Counter variable be incremented by a value other than 1 with each cycle of the loop. Here’s a loop that will execute four times, with the Counter variable taking the values 4, 7, 10, and 13:
    For I = 4 To 13 Step 3
       ...
    Next I
You can use negative Step values to count backward, which, of course, requires that the Stop value be smaller than Start. You can also use fractional values as long as the Counter variable is a floating point type. The following loop will count backward from 4 to 1 by increments of 0.25 (4, 3.75, 3.5, ... , 1.25, 1):
    For I = 4 To 1 Step -0.25
       ...
    Next I
If you want to terminate a loop early—that is, before the Counter variable exceeds Stop—you can use the Exit For statement. This is an extremely useful statement, because it lets you specify a variety of conditions that will terminate the loop in addition to the loop’s own count programming. Here’s a loop that will execute 10 times or until the variable X is less than 0:
    For I = 1 to 10
       ...
       If X < 0 Then Exit For
       ...
    Next I
Strictly speaking, you do not have to include the name of the Counter variable in the Next statement. Basic will automatically associate each Next statement with the immediately preceding For statement. I suggest, however, that you develop the habit of always including the Counter variable name with Next, to improve readability of the code. Do...Loop The Do...Loop structure is the most flexible of Basic’s loops. It allows the loop to execute until a specified condition is either True or False, and it allows the condition to be evaluated either at the start or the end of the loop. In its simplest form, a Do...Loop executes as long as a condition is True:
    Do While Condition
       ...
       statement block
       ...
    Loop
Condition is any logical expression. When execution first reaches the Do statement, Condition is evaluated. If it is False, execution passes to the statement following the Loop statement. If it is True, the block of statements is executed, execution returns to the Do statement, and Condition is evaluated again. You can also replace the While with the Until keyword to continue execution for as long as Condition is False, as shown here:
    Do Until Condition
       ...
       statement block
       ...
    Loop
Both of the previous examples perform the comparison at the start of the loop, which means it is possible for the statement block not to be executed—even once. For example, if Condition is False to begin with, a Do While Condition...Loop will not execute even once. If you want to be sure the loop executes at least once, you can place the Condition at the end of the loop. As before, you can use either the While or the Until keyword:
    Do
       ...
       statement block
       ...
    Loop While Condition

    Do
       ...
       statement block
       ...
    Loop Until Condition
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.